%{
 #include <ctype.h>
 #include <signal.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "out.h"
 #include <assert.h>
struct expr_t {
 short typ;
 int64_t val;
};
typedef struct expr_t expr_t;
struct item_t {
 struct item_t *
  i_next;
 short i_type;
 int64_t i_valu;
 char *i_name;
};
struct common_t {
 struct common_t *
  c_next;
 struct item_t *c_it;
 int64_t c_size;
};
typedef struct common_t common_t;
typedef struct item_t item_t;
struct sect_t {
 short s_flag;
 long s_base;
 long s_size;
 long s_comm;
 long s_zero;
 long s_lign;
 long s_foff;
 item_t *s_item;
 long s_gain;
};
typedef struct sect_t sect_t;
extern short pass ;
extern short peekc;
extern short unresolved;
extern long lineno;
extern short hllino;
extern short nerrors;
extern short sflag ;
extern char *progname;
extern char *modulename;
extern common_t *commons;
extern short uflag;
extern int64_t relonami;
extern short bflag;
extern char *aoutpath ;
extern FILE *input;
extern FILE *tempfile;
extern char *stringbuf;
extern int stringlen;
extern sect_t sect[64];
extern sect_t *DOTSCT;
extern long DOTVAL;
extern short DOTTYP;
extern unsigned short nname;
extern item_t *hashtab[(2*307)];
extern short hashindex;
extern item_t *last_it;
void item_set_nami(item_t *ip, unsigned short nami);
unsigned short item_get_nami(item_t *ip);
extern item_t *fb_ptr[4*10];
extern int nbits;
extern int bitindex;
extern item_t keytab[];
extern struct outhead outhead;
extern int curr_token;
int yyparse(void);
NORETURN void stop(void);
void newmodule(char *);
int yylex(void);
void putval(int);
int getval(int);
int nextchar(void);
int hash(const char *);
item_t *item_search(const char *);
void item_insert(item_t *, int);
item_t *item_alloc(int);
item_t *fb_alloc(int);
item_t *fb_shift(int);
void newequate(item_t *, int);
void newident(item_t *, int);
void newlabel(item_t *);
void newsect(item_t *);
void newbase(int64_t);
void newcomm(item_t *, int64_t);
void switchsect(int);
void align(int64_t);
void newrelo(int, int);
long new_string(const char *);
void newsymb(const char *, int, int, int64_t);
int64_t load(const item_t *);
int store(item_t *, int64_t);
char *remember(char *);
int combine(int, int, int);
int small(int, int);
void emit1(int);
void emit2(int);
void emit4(long);
void emitx(int64_t, int);
void emit8(int64_t);
void emitstr(int);
void emitf(int size, int negative);
void yyerror(const char *);
void nosect(void);
NORETURN void fatal(const char *, ...);
void serror(const char *, ...);
void warning(const char *, ...);
void nofit(void);
extern int co_id;
extern int mrg_1,mrg_2;
extern short ffew_1, ffew_2;
extern expr_t bd_1,bd_2,od_1,od_2;
extern int64_t bd_rel1,bd_rel2,od_rel1,od_rel2;
struct instr_t {
 short i_word;
 short i_relotype;
 int i_reloinfo;
 int64_t i_relonami;
};
typedef struct instr_t instr_t;
extern instr_t instr[30];
extern instr_t *instrp;
extern int dot_offset;
extern int curr_instr;
extern int curr_size;
extern short eamode[];
void emit_instr(void);
void t_emit2(short word, short relotype, int reloinfo, int64_t relnm);
void t_emit4(long words, short relotype, int reloinfo, int64_t relnm);
void ea_1(int sz, int bits);
void ea_2(int sz, int bits);
void checksize(int sz, int bits);
void check_fsize(int sz, int size);
void ch_sz_dreg(int size, int mode);
int checkscale(int64_t val);
void badoperand(void);
void shift_op(int opc, int sz);
void bitop(int opc);
void bitfield(int opc, int extension);
void add(int opc, int sz);
void and(int opc, int sz);
int to_dreg(int opc, int sz, int bits);
int from_dreg(int opc, int sz, int bits);
void cmp(int sz);
void link_instr(int sz, int areg);
void move(int sz);
void move_special(int sz);
void movem(int dr, int sz, int regs);
int reverse(int regs, int max);
void movep(int sz);
void branch(int opc, expr_t exp);
void cpbcc(int opc, expr_t exp);
void ea7071(int sz);
void fbranch(int opc, expr_t exp);
static item_t *o_it;
%}
%union {
 short y_word;
 int64_t y_valu;
 expr_t y_expr;
 item_t *y_item;
};
%token STRING
%token <y_item> IDENT
%token <y_item> FBSYM
%token <y_valu> CODE1
%token <y_valu> CODE2
%token <y_valu> CODE4
%token NUMBER0
%token NUMBER1
%token NUMBER2
%token NUMBER3
%token NUMBER4
%token <y_valu> NUMBER8
%token NUMBERF
%token DOT
%token EXTERN
%token <y_word> DATA
%token DATA8
%token <y_word> DATAF
%token <y_word> ASCII
%token SECTION
%token COMMON
%token BASE
%token SYMB
%token SYMD
%token ALIGN
%token ASSERT
%token SPACE
%token SEEK
%token <y_word> LINE
%token FILe
%token <y_word> LIST
%left OP_OO
%left OP_AA
%left '|'
%left '^'
%left '&'
%left OP_EQ OP_NE
%left '<' '>' OP_LE OP_GE
%left OP_LL OP_RR
%left '+' '-'
%left '*' '/' '%'
%nonassoc '~'
%type <y_valu> absexp optabs1 optabs2
%type <y_expr> expr
%type <y_item> id_fb
%token <y_word> SIZE
%token <y_word> DREG
%token <y_word> AREG
%token <y_word> MREG
%token <y_word> PC
%token <y_word> CREG
%token <y_word> SPEC
%token <y_word> ABCD
%token <y_word> ADDX
%token <y_word> ADD
%token <y_word> AND
%token <y_word> BITOP
%token <y_word> BITFIELD
%token <y_word> BF_TO_D
%token <y_word> BFINS
%token <y_word> SHIFT
%token <y_word> SZ_EA
%token <y_word> OP_EA
%token <y_word> OP_NOOP
%token <y_word> LEA
%token <y_word> DBR
%token <y_word> BR
%token <y_word> OP_EXT
%token <y_word> OP_RANGE
%token <y_word> TRAPCC
%token <y_word> PACK
%token <y_word> RTM
%token <y_word> CHK
%token <y_word> DIVMUL
%token <y_word> DIVL
%token <y_word> CMP
%token <y_word> MOVE
%token <y_word> MOVEM
%token <y_word> MOVEC
%token <y_word> MOVESP
%token <y_word> SWAP
%token <y_word> LINK
%token <y_word> UNLK
%token <y_word> TRAP
%token <y_word> EXG
%token <y_word> OP_IMM
%token <y_word> BKPT
%token <y_word> CALLM
%token <y_word> CAS
%token <y_word> CAS2
%token <y_word> CP
%token <y_word> CPBCC
%token <y_word> CPDBCC
%token <y_word> CPGEN
%token <y_word> CPSAVREST
%token <y_word> CPSCC
%token <y_word> CPTRAPCC
%token <y_word> FPCR
%token <y_word> FPREG
%token <y_word> FMOVE
%token <y_word> FMOVECR
%token <y_word> FMOVEM
%token <y_word> FDYADIC
%token <y_word> FMONADIC
%token <y_word> FSINCOS
%token <y_word> FBCC
%token <y_word> FDBCC
%token <y_word> FNOP
%token <y_word> FSCC
%token <y_word> FTST
%token <y_word> FSAVRES
%token <y_word> FTRAPCC
%token <y_word> FSIZE
%token <y_word> PFLUSHA
%token <y_word> PFLUSH
%token <y_word> PLOAD
%token <y_word> PTEST
%token <y_word> PMOVE
%type <y_word> bcdx op_ea regs rrange
%type <y_word> reg sizedef sizenon creg
%type <y_word> off_width off31 wid31 bd_areg_index
%type <y_word> areg_index areg scale cp_cond fc mask
%type <y_word> fsize fregs fcregs frlist frrange
%%
program :
 | program IDENT ':'
   { newident($2, DOTTYP); newlabel($2);}
 | program NUMBER8 ':'
   { if ($2 < 0 || $2 > 9) {
     serror("bad f/b label");
     $2 = 0;
    }
    newlabel(fb_shift((int)$2));
   }
 | program CODE1
   { emit1((int)$2); ;}
 | program CODE2
   { emit2((int)$2); ;}
 | program CODE4
   { emit4((long)$2); ;}
 | program operation ';'
 | program operation '\n'
   { lineno++; ; assert(relonami == 0);}
 | program '#' NUMBER8 STRING '\n'
   { lineno = $3;
    if (modulename) strncpy(modulename, stringbuf, 200 -1);
    ; assert(relonami == 0);
   }
 | program error '\n'
   { serror("syntax error"); yyerrok;
    lineno++; ; assert(relonami == 0);
   }
 ;
operation
 :
 | IDENT '=' expr
   {
    newequate($1, $3.typ);
    store($1, $3.val);
   }
 | SECTION IDENT
   { newsect($2);}
 | COMMON IDENT ',' absexp
   { newcomm($2, $4);}
 | BASE absexp
   { if (pass == 0) newbase($2);}
 | ASSERT expr
   { if ($2.val == 0 && pass == 2)
     warning("assertion failed");
   }
 | SYMB STRING ',' expr { o_it = last_it; }
  optabs2 optabs2
   { if ((sflag & 010) && (pass != 0)) {
     if (
      pass == 2
      &&
      ($4.typ & S_TYP) == S_UND
        ) {
      serror("expression undefined");
      relonami = -1;
     }
     if (
      (pass != 0)
      &&
      ($4.typ & S_COM)
        ) {
      $4.typ = S_CRS;
      $4.val = new_string(o_it->i_name);
      relonami = 0;
     }
     newsymb(
      *stringbuf ? stringbuf : (char *) 0,
      (short)(
       ($4.typ & (S_EXT|S_TYP))
       |
       ((unsigned short)$6<<8)
      ),
      (short)$7,
      $4.val
     );
    }
   }
 | SYMD STRING ',' absexp ',' absexp
   { if ((sflag & 010) && (pass != 0)) {
     newsymb(
      *stringbuf ? stringbuf : (char *) 0,
      (short)(
       (DOTTYP & (S_EXT|S_TYP))
       |
       ((unsigned short)$4<<8)
      ),
      (short)$6,
      (int64_t)DOTVAL
     );
    }
   }
 | LINE optabs1
   { if ((sflag & 020) && (pass != 0)) {
     if ($2)
      hllino = (short)$2;
     else
      hllino++;
     newsymb(
      (char *)0,
      (DOTTYP | S_LIN),
      hllino,
      (int64_t)DOTVAL
     );
    }
   }
 | FILe STRING
   { if ((sflag & 020) && (pass != 0)) {
     hllino = 0;
     newsymb(
      stringbuf,
      (DOTTYP | S_FIL),
      0,
      (int64_t)DOTVAL
     );
    }
   }
 | EXTERN externlist
 | ALIGN optabs1
   { align($2);}
 | SPACE absexp
   { if (DOTSCT == NULL)
     nosect();
    DOTVAL += $2;
    DOTSCT->s_zero += $2;
   }
 | SEEK absexp
   { if (DOTSCT == NULL)
     nosect();
    if ($2 < DOTVAL)
     serror("cannot move location counter backwards");
    if (pass == 0)
     DOTSCT->s_flag |= 2;
    DOTSCT->s_zero += $2 - DOTVAL;
    DOTVAL = $2;
   }
 | DATA datalist
 | DATA8 data8list
 | DATAF dataflist
 | ASCII STRING
   { emitstr($1);}
 ;
externlist
 : IDENT
   { $1->i_type |= S_EXT;}
 | externlist ',' IDENT
   { $3->i_type |= S_EXT;}
 ;
datalist
 : expr
   {
    if (1 != 0 && (pass != 0))
     newrelo($1.typ, (int)$<y_word>0|(RELBR|RELWR));
    emitx($1.val, (int)$<y_word>0);
   }
 | datalist ',' expr
   {
    if (1 != 0 && (pass != 0))
     newrelo($3.typ, (int)$<y_word>0|(RELBR|RELWR));
    emitx($3.val, (int)$<y_word>0);
   }
 ;
data8list
 : absexp
   { emit8($1);}
 | data8list ',' absexp
   { emit8($3);}
 ;
numberf
 : NUMBERF
   {
    emitf((int)$<y_word>-1, 0);
   }
 | '-' NUMBERF
   {
    emitf((int)$<y_word>-1, 1);
   }
 ;
dataflist
 : numberf
 | dataflist ',' numberf
 ;
expr : error
   { serror("expr syntax err");
    $$.val = 0; $$.typ = S_UND;
   }
 | NUMBER8
   { $$.val = $1;
    $$.typ = S_ABS;
   }
 | id_fb
   { $$.val = load($1);
    last_it = $1;
    $$.typ = $1->i_type & ~S_EXT;
   }
 | STRING
   { if (stringlen != 1)
     serror("too many chars");
    $$.val = stringbuf[0];
    $$.typ = S_ABS;
   }
 | '{' expr '}'
   { $$ = $2;}
 | expr OP_OO expr
   { $$.val = ($1.val || $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr OP_AA expr
   { $$.val = ($1.val && $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr '|' expr
   { $$.val = ($1.val | $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr '^' expr
   { $$.val = ($1.val ^ $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr '&' expr
   { $$.val = ($1.val & $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr OP_EQ expr
   { $$.val = ($1.val == $3.val);
    $$.typ = combine($1.typ, $3.typ, '>');
   }
 | expr OP_NE expr
   { $$.val = ($1.val != $3.val);
    $$.typ = combine($1.typ, $3.typ, '>');
   }
 | expr '<' expr
   { $$.val = ($1.val < $3.val);
    $$.typ = combine($1.typ, $3.typ, '>');
   }
 | expr '>' expr
   { $$.val = ($1.val > $3.val);
    $$.typ = combine($1.typ, $3.typ, '>');
   }
 | expr OP_LE expr
   { $$.val = ($1.val <= $3.val);
    $$.typ = combine($1.typ, $3.typ, '>');
   }
 | expr OP_GE expr
   { $$.val = ($1.val >= $3.val);
    $$.typ = combine($1.typ, $3.typ, '>');
   }
 | expr OP_RR expr
   { $$.val = ($1.val >> $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr OP_LL expr
   { $$.val = ($1.val << $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr '+' expr
   { $$.val = ($1.val + $3.val);
    $$.typ = combine($1.typ, $3.typ, '+');
   }
 | expr '-' expr
   { $$.val = ($1.val - $3.val);
    $$.typ = combine($1.typ, $3.typ, '-');
   }
 | expr '*' expr
   { $$.val = ($1.val * $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr '/' expr
   { if ($3.val == 0) {
     if (pass == 2)
      serror("divide by zero");
     $$.val = 0;
    } else
     $$.val = ($1.val / $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | expr '%' expr
   { if ($3.val == 0) {
     if (pass == 2)
      serror("divide by zero");
     $$.val = 0;
    } else
     $$.val = ($1.val % $3.val);
    $$.typ = combine($1.typ, $3.typ, 0);
   }
 | '+' expr %prec '*'
   { $$.val = $2.val;
    $$.typ = combine(S_ABS, $2.typ, 0);
   }
 | '-' expr %prec '*'
   { $$.val = -$2.val;
    $$.typ = combine(S_ABS, $2.typ, 0);
   }
 | '~' expr
   { $$.val = ~$2.val;
    $$.typ = combine(S_ABS, $2.typ, 0);
   }
 | DOT
   { $$.val = DOTVAL;
    $$.typ = DOTTYP|0x0400;
   }
 ;
id_fb : IDENT
 | FBSYM
 ;
absexp : expr
   { if (($1.typ & ~S_EXT) != S_ABS)
     serror("must be absolute");
    $$ = $1.val;
   }
 ;
optabs1
 :
   { $$ = 0;}
 | absexp
   { $$ = $1;}
 ;
optabs2
 :
   { $$ = 0;}
 | ',' absexp
   { $$ = $2;}
 ;
operation
 : { instrp = instr;
    dot_offset = 0;
    curr_instr = curr_token;
   }
  instruction
   { emit_instr();
   }
 ;
instruction
 : bcdx DREG ',' DREG
   { emit2($1 | $2 | $4<<9);}
 | bcdx '-' '(' AREG ')' ',' '-' '(' AREG ')'
   { emit2($1 | $4 | $9<<9 | 010);}
 | ADD sizedef ea_ea
   { add($1, $2);}
 | AND sizenon ea_ea
   { and($1, $2);}
 | SHIFT sizedef ea_ea
   { shift_op($1, $2);}
 | SHIFT sizedef ea
   { checksize($2, 2);
    t_emit2(($1 & 0177700) | mrg_2,0,0,(int64_t)0);
    ea_2(0100, 0x02|0x08);
   }
 | BR expr
   { branch($1, $2);}
 | DBR DREG ',' expr
   { t_emit2($1 | $2,0,0,(int64_t)0);
    $4.val -= (DOTVAL+dot_offset);
    if (!((((($4.val) + 0x8000L) & ~0xFFFFL) == 0))) nofit();
    t_emit2(((int)($4.val) & 0xFFFF),$4.typ,RELPC|RELO2,(int64_t)relonami);
   }
 | BITOP ea_ea
   { bitop($1);}
 | BITFIELD ea off_width
   { bitfield($1, $3);}
 | BF_TO_D ea off_width ',' DREG
   { bitfield($1, $3 | $5<<12);}
 | BFINS DREG ',' ea off_width
   { bitfield($1, $5 | $2<<12);}
 | DIVMUL sizedef ea ',' DREG
   { checksize($2, 2|4);
    if ($2 == 0100) {
     t_emit2((0140300^($1<<8))|mrg_2|$5<<9,0,0,(int64_t)0);
     ea_2(0100, 0x01);
    }
    else {
     t_emit2((046000 | ($1 & ~1)) | mrg_2,0,0,(int64_t)0);
     t_emit2(($1&1)<<11 | $5<<12 | $5,0,0,(int64_t)0);
     ea_2(0200, 0x01);
    }
   }
 | DIVMUL sizedef ea ',' DREG ':' DREG
   {
    checksize($2, 4);
    t_emit2((046000 | ($1 & ~1)) | mrg_2,0,0,(int64_t)0);
    t_emit2(($1&1)<<11 | $7<<12 | $5 | 02000,0,0,(int64_t)0);
    ea_2(0200, 0x01);
   }
 | DIVL sizedef ea ',' DREG ':' DREG
   {
    checksize($2, 4);
    t_emit2(($1 & ~1) | mrg_2,0,0,(int64_t)0);
    t_emit2(($1 & 1)<<11 | $7<<12 | $5,0,0,(int64_t)0);
    ea_2(0200, 0x01);
   }
 | LEA ea ',' AREG
   { t_emit2(040700 | mrg_2 | $4<<9,0,0,(int64_t)0);
    ea_2(0200, 0x04);
   }
 | op_ea ea
   { if (mrg_2==074)
     serror("bad adressing category");
    t_emit2(($1&0177700) | mrg_2,0,0,(int64_t)0);
    ea_2($1&0300, $1&017);
   }
 | OP_NOOP
   { emit2($1);}
 | OP_EXT SIZE DREG
   { checksize($2, ($1 & 0400) ? 4 : (2|4));
    emit2($1 | $2+0100 | $3);
   }
 | OP_RANGE sizedef ea ',' reg
   { t_emit2(0300 | ($2<<3) | mrg_2,0,0,(int64_t)0);
    t_emit2($1 | ($5<<12),0,0,(int64_t)0);
    ea_2($2, 0x04);
   }
 | TRAPCC SIZE imm
   { checksize($2, 2|4);
    t_emit2($1 | ($2>>6)+1,0,0,(int64_t)0);
    ea_2($2, 0);
   }
 | TRAPCC { emit2($1 | 4);}
 | PACK '-' '(' AREG ')' ',' '-' '(' AREG ')' ',' imm
   { t_emit2($1 | 8 | $4 | $9<<9,0,0,(int64_t)0);
    ea_2(0100, 0);
   }
 | PACK DREG ',' DREG ',' imm
   { t_emit2($1 | $2 | $4<<9,0,0,(int64_t)0);
    ea_2(0100, 0);
   }
 | CMP sizedef ea_ea
   { cmp($2);}
 | CHK sizedef ea ',' DREG
   { checksize($2, 2|4);
    t_emit2(040000 | mrg_2 | $5<<9 | ($2==0100 ? 0600 : 0400),0,0,(int64_t)0);
    ea_2($2, 0x01);
   }
 | MOVE sizenon ea_ea
   { move($2);}
 | MOVEM sizedef regs ',' notimmreg
   { movem(0, $2, $3);}
 | MOVEM sizedef notimmreg ',' regs
   { movem(1, $2, $5);}
 | MOVESP sizedef
   { curr_size = $1; }
  ea_ea
   { if ($1 == 0) {
     movep($2);
    } else if (mrg_1 <= 017) {
     t_emit2(007000 | $2 | mrg_2,0,0,(int64_t)0);
     t_emit2(mrg_1 << 12 | 04000,0,0,(int64_t)0);
     ea_2($2,0x08|0x02);
    } else if (mrg_2 <= 017) {
     t_emit2(007000 | $2 | mrg_1,0,0,(int64_t)0);
     t_emit2(mrg_2 << 12,0,0,(int64_t)0);
     ea_1($2,0x08|0x02);
    } else
     badoperand();
   }
 | MOVEC creg ',' reg
   { t_emit2(047172,0,0,(int64_t)0);
    t_emit2($2 | $4<<12,0,0,(int64_t)0);
   }
 | MOVEC reg ',' creg
   { t_emit2(047173,0,0,(int64_t)0);
    t_emit2($4 | $2<<12,0,0,(int64_t)0);
   }
 | EXG reg ',' reg
   { if (($2 & 010) == 0)
     emit2(
      (0140500|$4|$2<<9)
      +
      (($4&010)<<3)
     );
    else
     emit2(
      (0140610|$2|($4&07)<<9)
      -
      (($4&010)<<3)
     );
   }
 | SWAP DREG
   { emit2(044100 | $2);}
 | OP_IMM imm
   { t_emit2($1,0,0,(int64_t)0);
    ea_2(0100, 0);
   }
 | LINK sizenon AREG ',' imm
   { link_instr($2, $3);}
 | UNLK AREG
   { emit2(047130 | $2);}
 | TRAP '#' absexp
   { if (!(((($3) & ~((int)017)) == 0))) nofit();
    emit2(047100|((short)($3) & 017));
   }
 | BKPT '#' absexp
   { if (!(($3 & ~07) == 0)) nofit();
    emit2(044110 | ((short)($3) & 07));
   }
 | CALLM '#' absexp ',' ea
   { if (!((((($3) + 0x80) & ~((int)0xFF)) == 0))) nofit();
    t_emit2(03300 | mrg_2,0,0,(int64_t)0);
    t_emit2((short) $3,0,0,(int64_t)0);
    ea_2(0200, 0x04);
   }
 | RTM reg
   { emit2(03300 | $2);}
 | CAS sizedef DREG ',' DREG ',' ea
   { t_emit2(04300 | (($2+0100)<<3) | mrg_2,0,0,(int64_t)0);
    t_emit2($3 | ($5<<6),0,0,(int64_t)0);
    ea_2($2, 0x02|0x08);
   }
 | CAS2 sizedef DREG ':' DREG ',' DREG ':' DREG ','
     '(' reg ')' ':' '(' reg ')'
   { checksize($2 , 2|4);
    emit2(04374 | (($2+0100)<<3));
    emit2($3 | ($7<<6) | ($12<<12));
    emit2($5 | ($9<<6) | ($16<<12));
   }
 | fp_op
 | mm_op
 ;
bcdx : ABCD
 | ADDX sizedef
   { $$ = $1 | $2;}
 ;
creg : CREG
 | SPEC { if ($1 != 075)
     badoperand();
    $$ = 04000;
   }
 ;
off_width
 : '[' off31 ':' wid31 ']'
   { $$ = ($2<<6) | $4;
   }
 ;
off31 : DREG { $$ = 040 | $1;}
 | absexp { if (!(((($1) & ~((int)037)) == 0))) nofit();
    $$ = ((short)($1) & 037);
   }
 ;
wid31 : DREG { $$ = 040 | $1;}
 | absexp { if (!(((($1) & ~((int)037)) == 0) - 1)) nofit();
    $$ = ((short)($1) & 037);
   }
 ;
op_ea : OP_EA
 | SZ_EA sizedef
   { $$ = $1 | $2;}
 ;
regs : rrange
 | regs '/' rrange
   { $$ = $1 | $3;}
 ;
rrange : reg
   { $$ = 1<<$1;}
 | reg '-' reg
   { if ($1 > $3)
     badoperand();
    for ($$ = 0; $1 <= $3; $1++)
     $$ |= (1<<$1);
   }
 ;
ea : DREG
   { mrg_2 = $1;}
 | AREG
   { mrg_2 = 010 | $1;}
 | SPEC
   { mrg_2 = $1;}
 | notimmreg
 | imm
 ;
notimmreg
 :
   { mrg_2 = 0; ffew_2 = 0400; }
 notimmreg1
 ;
notimmreg1
 : '(' AREG ')'
   { mrg_2 = 020 | $2;}
 | '(' AREG ')' '+'
   { mrg_2 = 030 | $2;}
 | '-' '(' AREG ')'
   { mrg_2 = 040 | $3;}
 | '(' expr ')' sizenon
   { bd_2 = $2; ea7071($4);
    {bd_rel2 = relonami; relonami = 0;};
   }
 | '(' bd_areg_index ')'
   { if ((mrg_2 & 0300) == 0)
     ffew_2 |= 0100;
    if ( !(mrg_2 & 0400) &&
     (ffew_2 & 0300) == 0100 &&
     bd_2.typ==S_ABS && ((((bd_2.val) + 0x8000L) & ~0xFFFFL) == 0)
       )
     mrg_2 = (((int)(bd_2.val) & 0xFFFF)?050:020) | $2;
    else {
     mrg_2 = (mrg_2&0400)?073:(060 | $2);
     ffew_2 |= 060;
    }
   }
 | '(' '[' bd_areg_index ']' index_od ')'
   { switch(mrg_2 & 0300) {
    case 0:
     ffew_2 |= 0163;
     break;
    case 0300:
     serror("bad indexing");
    case 0100:
     ffew_2 |= 063; break;
    case 0200:
     ffew_2 |= 067; break;
    }
    mrg_2 = (mrg_2 & 0400) ? 073 : (060 | $3);
   }
 ;
imm : '#' expr
   { mrg_2 = 074; bd_2 = $2;
    {bd_rel2 = relonami; relonami = 0;};
   }
 ;
bd_areg_index
 :
   { $$ = 0; ffew_2 |= 0200;
    bd_2.typ = S_ABS; bd_2.val = (int64_t)0;
   }
 | expr { $$ = 0; ffew_2 |= 0200;
    bd_2 = $1;
    {bd_rel2 = relonami; relonami = 0;};
   }
 | areg_index
   { bd_2.typ = S_ABS; bd_2.val = (int64_t)0;
   }
 | expr ',' areg_index
   { $$ = $3; bd_2 = $1;
    {bd_rel2 = relonami; relonami = 0;};
   }
 ;
areg_index
 : areg
 | index { $$ = 0;
    ffew_2 |= 0200;
    mrg_2 |= 0100;
   }
 | areg ',' index
   { mrg_2 |= 0100;
   }
 ;
areg : AREG
 | PC { mrg_2 |= 0400;
    ffew_2 |= $1;
    $$ = 0;
   }
 ;
index : reg sizedef scale
   { checksize($2, 2|4);
    ffew_2 |= $1<<12 | ($2&0200)<<4 | $3;
   }
 ;
scale :
   { $$ = 0;}
 | '*' absexp
   { $$ = checkscale($2);}
 ;
index_od:
   { od_2.typ = S_ABS; od_2.val = (int64_t)0;}
 | ',' index
   { od_2.typ = S_ABS; od_2.val = (int64_t)0;
    mrg_2 |= 0200;
   }
 | ',' expr
   { od_2 = $2;
    {od_rel2 = relonami; relonami = 0;};
   }
 | ',' index ',' expr
   { od_2 = $4;
    mrg_2 |= 0200;
    {od_rel2 = relonami; relonami = 0;};
   }
 ;
reg : DREG
 | AREG
   { $$ = $1 | 010;}
 ;
sizedef :
   { $$ = 0100; curr_size = 0100; }
 | SIZE { curr_size = $1; $$ = $1; }
 ;
sizenon :
   { $$ = 0300;}
 | SIZE
 ;
ea_ea : ea ','
   { mrg_1 = mrg_2;
    bd_1 = bd_2;
    od_1 = od_2;
    ffew_1 = ffew_2;
    {bd_rel1 = bd_rel2; bd_rel2 = 0;};
    {od_rel1 = od_rel2; od_rel2 = 0;};
   }
  ea
 ;
fp_op : CP
   { co_id = $1; }
  fp_op1
 | { co_id = 01000; }
  fp_op1
 ;
fp_op1 : FMOVE fsize ea ',' FPCR
   { check_fsize($2, 00);
    if ((mrg_2&070) == 010 && $5 != 001)
     badoperand();
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0100000|($5<<10)),0,0,(int64_t)0);
    ea_2(0200, 0);
   }
 | FMOVE fsize FPCR ',' ea
   { check_fsize($2, 00);
    if ((mrg_2&070) == 010 && $3 == 001)
     badoperand();
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0120000|($3<<10)),0,0,(int64_t)0);
    ea_2(0200, 0x08);
   }
 | FMOVE fsize FPREG ',' FPREG
   { emit2(0170000|co_id);
    emit2(($3<<10)|($5<<7));
   }
 | FMOVE fsize ea ',' FPREG
   { ch_sz_dreg($2, mrg_2&070);
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0040000|($2<<10)|($5<<7)),0,0,(int64_t)0);
    ea_2(0200, 0x01);
   }
 | FMOVE fsize FPREG ',' ea
   { ch_sz_dreg($2, mrg_2&070);
    if ($2 == 03)
     serror("packed decimal needs k-factor");
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0060000|($2<<10)|($3<<7)),0,0,(int64_t)0);
    ea_2(0200, 0x01|0x08);
   }
 | FMOVE fsize FPREG ',' ea '{' '#' absexp '}'
   { check_fsize($2, 03);
    if (!(((((($8)+64) & ~((int)0177)) == 0)))) nofit();
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0066000|($3<<7)|((short)($8) & 0177)),0,0,(int64_t)0);
    ea_2(0200, 0x02|0x01|0x08);
   }
 | FMOVE fsize FPREG ',' ea '{' DREG '}'
   { check_fsize($2, 03);
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0076000|($3<<7)|($7<<4)),0,0,(int64_t)0);
    ea_2(0200, 0x02|0x01|0x08);
   }
 | FMOVECR fsize '#' absexp ',' FPREG
   { if (!(((($4) & ~((int)0177)) == 0))) nofit();
    check_fsize($2, 02);
    emit2(0170000|co_id);
    emit2(056000|($6<<7)|((short)($4) & 0177));
   }
 | FMOVEM FSIZE fregs ',' notimmreg
   { check_fsize($2, 02);
    if ((mrg_2&070) == 030)
     serror("bad addressing category");
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2(0160000 | (((mrg_2&070)==040 || ($3&04000)) ? $3 : (010000|reverse($3,8))),0,0,(int64_t)0);
    ea_2(0200, 0x02|0x08);
   }
 | FMOVEM FSIZE notimmreg ',' fregs
   { check_fsize($2, 02);
    if ((mrg_2&070) == 040)
     serror("bad addressing category");
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0150000|(($5&04000)?$5:reverse($5,8))),0,0,(int64_t)0);
    ea_2(0200, 0x02);
   }
 | FMOVEM SIZE fcregs ',' ea
   { checksize($2, 4);
    if ((mrg_2&070) == 1 && $3!= 02000)
     serror("bad addressing category");
    if ((mrg_2 & 070) == 0 &&
        $3 != 02000 && $3 != 04000 && $3 != 010000)
     serror("bad addressing category");
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0120000|$3),0,0,(int64_t)0);
    ea_2(0200, 0x08);
   }
 | FMOVEM SIZE ea ',' fcregs
   { checksize($2, 4);
    if ((mrg_2&070) == 1 && $5!= 02000)
     serror("bad addressing category");
    if ((mrg_2 & 070) == 0 &&
        $5 != 02000 && $5 != 04000 && $5 != 010000)
     serror("bad addressing category");
    t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0100000|$5),0,0,(int64_t)0);
    ea_2(0200, 0);
   }
 | FDYADIC fsize ea ',' FPREG
   { t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0040000|($2<<10)|($5<<7)|$1),0,0,(int64_t)0);
    ch_sz_dreg($2, mrg_2&070);
    ea_2(0200, 0x01);
   }
 | FDYADIC fsize FPREG ',' FPREG
   { check_fsize($2, 02);
    emit2(0170000|co_id);
    emit2(($3<<10)|($5<<7)|$1);
   }
 | FMONADIC fsize ea ',' FPREG
   { t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0040000|($2<<10)|($5<<7)|$1),0,0,(int64_t)0);
    ch_sz_dreg($2, mrg_2&070);
    ea_2(0200, 0x01);
   }
 | FMONADIC fsize FPREG ',' FPREG
   { check_fsize($2, 02);
    emit2(0170000|co_id);
    emit2(($3<<10)|($5<<7)|$1);
   }
 | FMONADIC fsize FPREG
   { check_fsize($2, 02);
    emit2(0170000|co_id);
    emit2(($3<<10)|($3<<7)|$1);
   }
 | FSINCOS fsize ea ',' FPREG ':' FPREG
   { t_emit2(0170000|co_id|mrg_2,0,0,(int64_t)0);
    t_emit2(0040000|($2<<10)|($7<<7)|$1|$5,0,0,(int64_t)0);
    ea_2(0200, 0x01);
   }
 | FSINCOS fsize FPREG ',' FPREG ':' FPREG
   { check_fsize($2, 02);
    emit2(0170000|co_id);
    emit2(($3<<10)|($7<<7)|$1|$5);
   }
 | FBCC expr
   { fbranch($1, $2);}
 | FDBCC DREG ',' expr
   { t_emit2(0170110|co_id|$2,0,0,(int64_t)0);
    t_emit2($1,0,0,(int64_t)0);
    $4.val -= (DOTVAL+dot_offset);
    if (!((((($4.val) + 0x8000L) & ~0xFFFFL) == 0))) nofit();
    t_emit2(((int)($4.val) & 0xFFFF),$4.typ,RELPC|RELO2,(int64_t)relonami);
   }
 | FNOP
   { emit2(0170200|co_id);
    emit2(0);
   }
 | FSCC ea
   { t_emit2(0170100|co_id|mrg_2,0,0,(int64_t)0);
    t_emit2($1,0,0,(int64_t)0);
    ea_2(0000, 0x01|0x08);
   }
 | FTST fsize ea
   { t_emit2((0170000|co_id|mrg_2),0,0,(int64_t)0);
    t_emit2((0040072|($2<<10)),0,0,(int64_t)0);
    ch_sz_dreg($2, mrg_2&070);
    ea_2(0200, 0x01);
   }
 | FTST fsize FPREG
   { check_fsize($2, 02);
    emit2(0170000|co_id);
    emit2(($3<<10)|072);
   }
 | FSAVRES ea
   { if ((mrg_2&070) == ($1&070))
     badoperand();
    t_emit2((0170000|co_id|($1&0700)|mrg_2),0,0,(int64_t)0);
    ea_2(0, $1&07);
   }
 | FTRAPCC
   { emit2(0170174|co_id);
    emit2($1);
   }
 | FTRAPCC SIZE imm
   { checksize($2, 2|4);
    t_emit2((0170170|co_id|($2==0200?03:02)),0,0,(int64_t)0);
    t_emit2($1,0,0,(int64_t)0);
    ea_2($2,0);
   }
 ;
fregs : DREG
   { $$ = 04000 | $1 << 4; }
 | frlist
 ;
frlist : frrange
 | frlist '/' frrange
   { $$ = $1 | $3;}
 ;
frrange : FPREG
   { $$ = 1 << $1; }
 | FPREG '-' FPREG
   { if ($1 > $3)
     badoperand();
    for ($$ = 0; $1 <= $3; $1++)
     $$ |= (1 << $1);
   }
 ;
fcregs : FPCR
   { $$ = $1 << 10; }
 | fcregs '/' FPCR
   { $$ = $1 | ($3 << 10); }
 ;
fsize :
   { $$ = 02; }
 | SIZE
   { if ($1 == 0200)
     $$ = 00;
    else if ($1 == 0100)
     $$ = 04;
    else $$ = 06;
   }
 | FSIZE
 ;
mm_op : CP
   { co_id = $1; }
  mm_op1
 | { co_id = 00000; }
  mm_op1
 ;
mm_op1 :
  CPBCC cp_cond expr
   { cpbcc($1 | co_id | $2, $3);
   }
 | CPDBCC cp_cond DREG ',' expr
   { t_emit2($1 | co_id | $3,0,0,(int64_t)0);
    $5.val -= (DOTVAL+dot_offset);
    if (!((((($5.val) + 0x8000L) & ~0xFFFFL) == 0))) nofit();
    t_emit2(((int)($5.val) & 0xFFFF),$5.typ,RELPC|RELO2,(int64_t)relonami);
   }
 | CPGEN
   { t_emit2($1 | co_id,0,0,(int64_t)0);
   }
 | CPSAVREST ea
   { t_emit2($1 | co_id | mrg_2,0,0,(int64_t)0);
    if ($1 & 0100) {
     ea_2(0100, (mrg_2 & 070)==030 ? 0 : 0x04);
    }
    else ea_2(0100,(mrg_2 & 070)==020 ? 0 : 0x04|0x08);
   }
 | CPSCC cp_cond ea
   { t_emit2($1 | co_id | mrg_2,0,0,(int64_t)0);
    t_emit2($2,0,0,(int64_t)0);
    ea_2(0000,0x01|0x08);
   }
 | CPTRAPCC cp_cond SIZE imm
   { checksize($3,2|4);
    t_emit2($1 | co_id | ($3>>6)+1,0,0,(int64_t)0);
    t_emit2($2,0,0,(int64_t)0);
    ea_2($3, 0);
   }
 | CPTRAPCC cp_cond
   { emit2($1 | co_id | 4);
    emit2($2);
   }
 | PFLUSHA
   { emit2(0170000);
    emit2($1);
   }
 | PFLUSH fc ',' mask
   { emit2(0170000);
    emit2($1|010000|($4<<5)|$2);
   }
 | PFLUSH fc ',' mask ',' ea
   { t_emit2(0170000|mrg_2,0,0,(int64_t)0);
    t_emit2($1|014000|($4<<5)|$2,0,0,(int64_t)0);
    ea_2(0200, 0x01|0x04);
   }
 | PTEST fc ',' ea ',' mask
   { t_emit2(0170000|mrg_2,0,0,(int64_t)0);
    t_emit2($1|($6<<10)|$2,0,0,(int64_t)0);
    ea_2(0200, 0x01|0x04);
   }
 | PTEST fc ',' ea ',' mask ',' AREG
   { t_emit2(0170000|mrg_2,0,0,(int64_t)0);
    t_emit2($1|($6<<10)|$2|0400|($8<<5),0,0,(int64_t)0);
    ea_2(0200, 0x01|0x04);
   }
 | PLOAD fc ',' ea
   { t_emit2(0170000|mrg_2,0,0,(int64_t)0);
    t_emit2($1|$2,0,0,(int64_t)0);
    ea_2(0200, 0x01|0x04);
   }
 | PMOVE MREG ',' ea
   { t_emit2(0170000|mrg_2,0,0,(int64_t)0);
    t_emit2($1|$2|01000,0,0,(int64_t)0);
    ea_2(0200, 0x01|0x04);
   }
 | PMOVE ea ',' MREG
   { t_emit2(0170000|mrg_2,0,0,(int64_t)0);
    t_emit2($1|$4,0,0,(int64_t)0);
    ea_2(0200, 0x01|0x04);
   }
 ;
mask : '#' absexp
   { if (!((((($2)-1) & ~((int)07)) == 0))) nofit();
    $$ = ((short)($2) & 07);
   }
 ;
fc : '#' absexp
   { if (!((((($2)-1) & ~((int)07)) == 0))) nofit();
    $$ = (020|((short)($2) & 07));
   }
 | DREG
   { $$ = (010|$1); }
 | CREG
   { if ($1 > 1) serror("illegal control register");
    $$ = ($1&01);
   }
 ;
cp_cond : DOT absexp
   { if (!(((($2) & ~((int)077)) == 0))) nofit();
    $$ = ((short)($2) & 077);
   }
 ;
%%
